home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / double.el < prev    next >
Lisp/Scheme  |  1994-06-22  |  6KB  |  200 lines

  1. ;;; double.el - Support for keyboard remapping with double clicking.
  2.  
  3. ;; Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
  6. ;; Keywords: i18n
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This mode is intended for use with languages that adds a small
  27. ;; number of extra letters not available on the keyboard.
  28. ;; 
  29. ;; Examples includes Scandinavian and German with an US keyboard.
  30. ;;
  31. ;; The idea is that certain keys are overloaded.  When you press it
  32. ;; once it will insert one string, and when you press it twice the
  33. ;; string will be replaced by another.  This can be used for mapping
  34. ;; keys on a US keyboard to generate characters according to the local
  35. ;; keyboard convention when pressed once, and according to US keyboard
  36. ;; convetion when pressed twice. 
  37. ;;
  38. ;; To use this mode, you must define the variable `double-map' and
  39. ;; then enable double mode with `M-x double-mode'.  Read the
  40. ;; documentation for both of them.
  41. ;;
  42. ;; The default mapping is for getting Danish/Norwegian keyboard layout
  43. ;; using ISO Latin 1 on a US keyboard.
  44. ;;
  45. ;; Requires FSF Emacs 19.20 or later.
  46. ;;
  47. ;; Imprtant node: While I would like to hear comments, bug reports,
  48. ;; suggestions, please do @strong{not} expect me to put other mappings
  49. ;; that the default into this file.  There are billions and billions
  50. ;; of such mappings, and just supporting the most common would
  51. ;; increase the size of this nice small file manyfold.
  52.  
  53. ;;; ChangeLog: 
  54.  
  55. ;; * 1994-06-21         Per Abrahamsen
  56. ;;      Added `double-prefix-only'.
  57. ;; * 1994-02-28         Per Abrahamsen
  58. ;;      Use 127 instead of 'delete to delete a character.
  59. ;; * 1994-02-03        Per Abrahamsen
  60. ;;    Created.
  61.  
  62. ;;; Code:
  63.  
  64. (defvar double-map
  65.   '((?\; "\346" ";")
  66.     (?\' "\370" "'")
  67.     (?\[ "\345" "[")
  68.     (?\: "\306" ":")
  69.     (?\" "\330" "\"")
  70.     (?\{ "\305" "{"))
  71.   "Alist of key translations activated by double mode.
  72.  
  73. Each entry is a list with three elements:
  74. 1. The key activating the translation.
  75. 2. The string to be inserted when the key is pressed once.
  76. 3. The string to be inserted when the key is pressed twice.")
  77.  
  78. (defvar double-prefix-only t
  79.   "*Non-nil means that double mode mapping only works for prefix keys.
  80. That is, for any key `X' in `double-map',  `X' alone will be mapped
  81. but not `C-u X' or `ESC X' since the X is not the prefix key.")
  82.  
  83. ;;; Read Event
  84.  
  85. (defvar double-last-event nil)
  86. ;; The last key that generated a double key event.
  87.  
  88. (defun double-read-event (prompt)
  89.   ;; Read an event
  90.   (if isearch-mode (isearch-update))
  91.   (if prompt
  92.       (prog2 (message "%s%c" prompt double-last-event)
  93.       (read-event)
  94.     (message ""))
  95.     (read-event)))
  96.  
  97. (global-set-key [ignore] '(lambda () (interactive)))
  98.  
  99. (or (boundp 'isearch-mode-map)
  100.     (load-library "isearch"))
  101.  
  102. (define-key isearch-mode-map [ignore] 
  103.   (function (lambda () (interactive) (isearch-update))))
  104.  
  105. (defun double-translate-key (prompt)
  106.   ;; Translate input events using double map.
  107.   (let ((key last-input-char))
  108.     (cond (unread-command-events
  109.        ;; Artificial event, ignore it.
  110.        (vector key))
  111.       ((and double-prefix-only
  112.         (> (length (this-command-keys)) 1))
  113.        ;; This is not a prefix key, ignore it.
  114.        (vector key))
  115.       ((eq key 'magic-start)
  116.        ;; End of generated event.  See if he will repeat it...
  117.        (let ((new (double-read-event prompt))
  118.          (entry (assoc double-last-event double-map)))
  119.          (if (eq new double-last-event)
  120.          (progn 
  121.            (setq unread-command-events
  122.              (append (make-list (1- (length (nth 1 entry)))
  123.                         127)
  124.                  (nth 2 entry)
  125.                  '(magic-end)))
  126.            (vector 127))
  127.            (setq unread-command-events (list new))
  128.            [ignore])))
  129.       ((eq key 'magic-end)
  130.        ;; End of double event.  Ignore.
  131.        [ignore])
  132.       (t
  133.        ;; New key.
  134.        (let ((exp (nth 1 (assoc key double-map))))
  135.          (setq double-last-event key)
  136.          (setq unread-command-events
  137.            (append (substring exp 1) '(magic-start)))
  138.          (vector (aref exp 0)))))))
  139.  
  140. ;;; Key Translation Map
  141.  
  142. (defvar default-key-translation-map
  143.   (or key-translation-map (make-sparse-keymap))
  144.   "Key translation you want to have effect, regardless of double mode.
  145. This will default to the value of `key-translation-map' when double was
  146. first loaded.")
  147.  
  148. (make-variable-buffer-local 'key-translation-map)
  149.  
  150. (defun double-setup ()
  151.   ;; Setup key-translation-map as indicated by `double-map'.
  152.   (setq key-translation-map (copy-keymap default-key-translation-map))
  153.   (mapcar (function (lambda (entry)
  154.             (define-key key-translation-map (vector (nth 0 entry))
  155.                     'double-translate-key)))
  156.       (append double-map '((magic-start) (magic-end)))))
  157.  
  158. ;;; Mode
  159.  
  160. (defvar double-mode nil)
  161. ;; Indicator for the double mode.
  162.   (make-variable-buffer-local 'double-mode)
  163.  
  164. (or (assq 'double-mode minor-mode-alist)
  165.     (setq minor-mode-alist
  166.       (cons '(double-mode (" " double-mode-name)) minor-mode-alist)))
  167.  
  168. (defvar double-mode-name "Double")
  169. ;; Name of current double mode.
  170.   (make-variable-buffer-local 'double-mode-name)
  171.  
  172. ;;;###autoload
  173. (defun double-mode (arg)
  174.   "Toggle double mode.
  175. With prefix arg, turn double mode on iff arg is positive.
  176.  
  177. When double mode is on, some keys will insert will insert different
  178. strings when pressed twice.  See variable `double-map' for details."
  179.   (interactive "P")
  180.   (if (or (and (null arg) double-mode)
  181.       (<= (prefix-numeric-value arg) 0))
  182.       ;; Turn it off
  183.       (if double-mode
  184.       (progn
  185.         (let ((double-map))
  186.           (double-setup))
  187.         (setq double-mode nil)
  188.         (set-buffer-modified-p (buffer-modified-p))))
  189.     ;;Turn it on
  190.     (if double-mode
  191.     ()
  192.       (double-setup)
  193.       (setq double-mode t)
  194.       (set-buffer-modified-p (buffer-modified-p)))))
  195.  
  196. (provide 'double)
  197.  
  198. ;;; double.el ends here
  199.  
  200.